package com.woniu.order.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.woniu.exception.LXLGException;
import com.woniu.order.common.CommentResultCode;
import com.woniu.order.common.enums.CommentStatusEnum;
import com.woniu.order.infra.model.Comment;
import com.woniu.order.infra.mapper.CommentMapper;
import com.woniu.order.infra.model.CommentAttachment;
import com.woniu.order.service.CommentAttachmentService;
import com.woniu.order.service.CommentService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.order.service.dto.CommentDto;
import com.woniu.order.service.param.AddCommentServiceParam;
import com.woniu.order.service.param.AdditionalCommentServiceParam;
import com.woniu.result.Result;
import com.woniu.user.client.UserInfoClient;
import com.woniu.user.client.result.UserResultInfo;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 评价表 服务实现类
 * </p>
 *
 * @author Lucas
 * @since 2022-11-26
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Resource
    private CommentAttachmentService commentAttachmentService;

    @Resource
    private UserInfoClient userInfoClient;


    /**
     * 根据用户ID查询此用户的所有评论
     *
     * @param userId
     * @return
     */
    @Override
    public List<CommentDto> searchCommentsByUserId(Integer userId) {
        QueryWrapper<Comment> qw = new QueryWrapper<>();
        qw.eq("user_id", userId).eq("state",CommentStatusEnum.NORMAL);
        qw.orderByDesc("comment_time");
        List<Comment> comments = list(qw);
        List<CommentDto> commentDtos = BeanUtil.copyToList(comments, CommentDto.class);
        return commentDtos;
    }

    /**
     * 根据商品ID查询关于此商品的评论
     *
     * @param productId
     * @return
     */
    @Override
    public List<CommentDto> searchCommentsByProductId(Integer productId) {
        QueryWrapper<Comment> qw = new QueryWrapper<>();
        qw.eq("product_id", productId).eq("state",CommentStatusEnum.NORMAL);
        qw.orderByDesc("comment_time");
        List<Comment> comments = list(qw);
        List<CommentDto> commentDtos = BeanUtil.copyToList(comments, CommentDto.class);
        return commentDtos;
    }

    /**
     * 评价
     *
     * @param addCommentServiceParam
     * @return
     */
    @Override
    public Boolean addComment(AddCommentServiceParam addCommentServiceParam) {
        Integer userId = addCommentServiceParam.getUserId();
        Result<UserResultInfo> userResultInfoResult = userInfoClient.searchUserInfo(userId);
        String nickname = userResultInfoResult.getData().getUserNickname();
        QueryWrapper<Comment> qw = new QueryWrapper<>();
        qw.eq("user_id", userId).eq("order_id", addCommentServiceParam.getOrderId()).eq("product_id", addCommentServiceParam.getProductId());
        Comment comment1 = getOne(qw);
        if (comment1 != null) {
            throw new LXLGException(CommentResultCode.HAVE_COMMENTED);
        }
        Comment comment = BeanUtil.copyProperties(addCommentServiceParam, Comment.class);
        comment.setUserNickname(nickname);
        comment.setCommentTime(new Date());
        comment.setAdditionalContent(null);
        comment.setAdditionalTime(null);
        comment.setState(CommentStatusEnum.NORMAL.getCode());
        save(comment);
        CommentAttachment commentAttachment = new CommentAttachment();
        commentAttachment.setCommentId(comment.getCommentId());
        commentAttachmentService.update(commentAttachment, new UpdateWrapper<CommentAttachment>().eq("file_path", comment.getCommentPicture()));
        return true;
    }

    /**
     * 追加评论
     *
     * @param additionalCommentServiceParam
     * @return
     */
    @Override
    public Boolean addAdditionalComment(AdditionalCommentServiceParam additionalCommentServiceParam) {
        Comment comment = getById(additionalCommentServiceParam.getCommentId());
        if (comment == null) {
            throw new LXLGException(CommentResultCode.NO_COMMENTS_YET);
        }
        if(comment.getAdditionalContent()!=null){
            throw new LXLGException(CommentResultCode.NOT_MORE_THAN_ONCE);
        }
        comment.setAdditionalContent(additionalCommentServiceParam.getContent());
        comment.setAdditionalTime(new Date());
        return updateById(comment);
    }
}
